//
//  ByLotteryNoticeVC.m
//  caipiao_ios
//
//  Created by mc on 2018/7/3.
//  Copyright © 2018年 ZhangZhanbin. All rights reserved.
//

#import "ByLotteryNoticeVC.h"
#import "ByNoticeNumberCell.h"
#import "ByLotteryNoticeDetailVC.h"
#import "ByNoticeBallCell.h"
#import "ByBallNoticeDetailVC.h"

@interface ByLotteryNoticeVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;


@end

@implementation ByLotteryNoticeVC

- (NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray arrayWithCapacity:1];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"开奖公告";
    
    [self initViews];
    
   
    ByCoreRefreshGifHeader *header = [ByCoreRefreshGifHeader headerWithRefreshingBlock:^{
        [self loadData];
    }];
    self.tableView.mj_header = header;
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    
    if (BY_USER) {
        
         [self loadData];
        
    }
   
}

- (void)loadData{
    
    if (!BY_USER) {
        [self.tableView.mj_header endRefreshing];
        return;
    }
    
    [ByCoreToolAlert showLoadind:@"加载中"];
    [ByApiOther drawNoticeSuccess:^(ByNetBaseResult *result) {
        [ByCoreToolAlert hideHud];
        [self.tableView.mj_header endRefreshing];
        SLog(@"开奖公告 = %@", result.data);
        [self.dataArray removeAllObjects];
        for (NSDictionary *dic in result.data[@"data"][@"data"]) {
            ByNetModel *netModel = [[ByNetModel alloc] initContentWithDic:dic];
            NSMutableArray *redArray = [ByLotteryAlgorithmHandle getModelsArrayWithArray:dic[@"red_num"] WithColor:BY_THEME_COLOR];
            NSMutableArray *blueArray = [ByLotteryAlgorithmHandle getModelsArrayWithArray:dic[@"blue_num"] WithColor:BY_Blue_COLOR];
            NSMutableArray *numArray = [NSMutableArray array];
            for (ByNetModel *model in redArray) {
                [numArray addObject:model];
            }
            
            for (ByNetModel *model in blueArray) {
                [numArray addObject:model];
            }
            netModel.numArray = numArray;
            
            //双色球
            if (netModel.category_id.integerValue == 1001){
                
                NSArray *contentArray = dic[@"content"];
                netModel.contentArray = contentArray;
                
            }else if (netModel.category_id.integerValue == 2001){
                
                netModel.contentDic = dic[@"content"];
            }
            
            
            [self.dataArray addObject:netModel];
        }
        [self.tableView  reloadData];
        
    } failure:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showFailureWithText:result.msg];
        [self.tableView.mj_header endRefreshing];
    }];
    
}

- (void)initViews{
    
    self.view.backgroundColor = BY_BACKGROUND_COLOR;
    self.tableView = [[UITableView alloc] init];
    self.tableView.backgroundColor = BY_BACKGROUND_COLOR;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(0);
        make.right.equalTo(self.view).offset(0);
        if (@available(ios 11.0,*)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).offset(0);
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(-49);
        } else {
            make.top.equalTo(self.view).offset(0);
            make.bottom.equalTo(self.view).offset(-49);
        }
    }];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.dataArray.count == 4) {
        return 4;
    }
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ByNetModel *netModel = [self.dataArray objectAtIndex:indexPath.row];
    
    if (netModel.category_id.integerValue == 2001 || netModel.category_id.integerValue == 1001) {
        
        ByNoticeNumberCell *cell = [ByNoticeNumberCell cellWithTableView:tableView];
        
        cell.netModel = netModel;
        
        return cell;
        
   //竞彩足球
    }else if (netModel.category_id.integerValue == 3010 || netModel.category_id.integerValue == 3001){
        
        ByNoticeBallCell *cell = [ByNoticeBallCell cellWithTableView:tableView];
        cell.netModel = netModel;
        return cell;
        
    }
    else {
        
        static NSString *cellIdentifier = @"cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        }
        [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        return cell;
        
        
    }
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 10 + 35 + 60 + 25 + 60 + 10;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ByNetModel *netModel = [self.dataArray objectAtIndex:indexPath.row];
    
    if (netModel.category_id.integerValue == 2001 || netModel.category_id.integerValue == 1001) {
        
        ByLotteryNoticeDetailVC *vc = [[ByLotteryNoticeDetailVC alloc] init];
        vc.netModel = netModel;
        [self by_normalPush:vc hasNavi:YES];
        
    }else if (netModel.category_id.integerValue == 3010 || netModel.category_id.integerValue == 3001){
        
        ByBallNoticeDetailVC *vc = [[ByBallNoticeDetailVC alloc] init];
        vc.netModel = netModel;
        [self by_normalPush:vc hasNavi:YES];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
